<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>病单报告管理 - 医疗预约系统后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <link rel="stylesheet" href="statics/css/common-styles.css">
</head>
<body>
<!-- 侧边栏 -->
<%@include file="../common/sidebar.jsp"%>

<!-- 主内容区域 -->
<div class="main-content">
    <!-- 顶部导航 -->
<%@include file="../common/top.jsp"%>

    <!-- 内容区域 -->
    <div class="content">
        <div class="function-page">
            <!-- 页面标题 -->
            <div class="page-header">
                <h1>病单报告管理</h1>
            </div>

            <!-- 查询表单 -->
            <div class="search-filter">
                <form id="reportFindForm" action="report/find" method="get">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="symptoms">症状：</label>
                            <input type="text" id="symptoms" name="symptoms" value="${condition.symptoms}" placeholder="请输入症状关键词" class="form-control"/>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> 查询
                            </button>
                            <span class="search-info">总计 ${pageInfo.total} 条病单报告</span>
                        </div>
                    </div>
                </form>
            </div>

            <!-- 病单报告列表 -->
            <div class="data-section">
                <table class="data-table">
                <thead>
                <tr>
                    <th>病单号</th>
                    <th>用户姓名</th>
                    <th>科室</th>
                    <th>症状</th>
                    <th>医生建议</th>
                    <th>生成时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:choose>
                    <c:when test="${pageInfo.list != null && not empty pageInfo.list}">
                        <c:forEach items="${pageInfo.list}" var="report">
                            <tr>
                                <td>${report.reportid}</td>
                                <td>${report.user.username}</td>
                                <td>${report.appointment.visit.department.deptname}</td>
                                <td>${report.symptoms}</td>
                                <td>${report.doctoradvice}</td>
                                <td>${report.createtime}</td>
                                <td>
                                    <a href="javascript:void(0)" class="btn btn-secondary btn-sm" onclick="findById(${report.reportid})"><i class="fas fa-search"></i> 查看</a>
                                    <!-- 只有非病人用户可以看到编辑和删除按钮 -->
                                    <c:if test="${loginAdmin == null || loginAdmin.type != 1}">
                                        <a href="javascript:void(0)" class="btn btn-primary btn-sm" onclick="findById(${report.reportid})"><i class="fas fa-edit"></i> 编辑</a>
                                        <a href="javascript:void(0)" class="btn btn-danger btn-sm" onclick="deleteReport(${report.reportid})"><i class="fas fa-trash-alt"></i> 删除</a>
                                    </c:if>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <tr>
                            <td colspan="7" class="no-data">
                                <i class="fas fa-search"></i> 没有找到匹配的病单报告
                            </td>
                        </tr>
                    </c:otherwise>
                </c:choose>
                </tbody>
            </table>
            </div>
            
            <!-- 分页控件 -->
            <span class="pagination-info">共 ${pageInfo.total} 条记录，当前第 ${pageInfo.pageNum} / ${pageInfo.pages} 页</span>
            
            <style>
                .no-data {
                    text-align: center;
                    padding: 40px 0;
                    color: #666;
                    background-color: #f9f9f9;
                }
                
                .no-data i {
                    font-size: 24px;
                    margin-right: 8px;
                    color: #999;
                }
            </style>
            <div class="pagination-container">
                <div class="pagination">
                    <a href="report/find?pageNum=1&symptoms=${condition.symptoms}" 
                       class="pagination-link">
                       <i class="fas fa-angle-double-left"></i> 首页
                    </a>
                    <a href="report/find?pageNum=${pageInfo.pageNum-1}&symptoms=${condition.symptoms}" 
                       class="pagination-link ${pageInfo.pageNum == 1 ? 'disabled' : ''}">
                       <i class="fas fa-angle-left"></i> 上一页
                    </a>
                    
                    <!-- 页码遍历 -->
                    <c:forEach begin="1" end="${pageInfo.pages}" var="num">
                        <a href="report/find?pageNum=${num}&symptoms=${condition.symptoms}" 
                           class="pagination-link ${pageInfo.pageNum == num ? 'active' : ''}">
                           ${num}
                        </a>
                    </c:forEach>
                    
                    <c:if test="${pageInfo.pageNum < pageInfo.pages}">
                        <a href="report/find?pageNum=${pageInfo.pageNum+1}&symptoms=${condition.symptoms}" 
                           class="pagination-link">
                           <i class="fas fa-angle-right"></i> 下一页
                        </a>
                        <a href="report/find?pageNum=${pageInfo.pages}&symptoms=${condition.symptoms}" 
                           class="pagination-link">
                           <i class="fas fa-angle-double-right"></i> 末页
                        </a>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function findById(reportId) {
        window.location.href = "report/findById?reportId=" + reportId;
    }
    
    function deleteReport(reportId) {
        if (confirm('确定要删除这个病单报告吗？')) {
            window.location.href = 'report/delete?reportId=' + reportId;
        }
    }
</script>
</body>
</html>